﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Incubator.Controls
{
    
    public partial class ChooseQuiz : System.Web.UI.UserControl
    {

        
        int category_id = 0;
        List<webRef.Quiz> quizs = new List<webRef.Quiz>();
        string c0 = "Mathematics";
        string c1 = "Geography";
        string c2 = "History";
        string c3 = "Sport";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["UserID"] == null || (int)Session["UserID"] == -1)
                Response.Redirect("main.aspx");

            if (Session["category_id"] != null && Page.IsPostBack)
            {
                category_id = (int)Session["category_id"];
                showQuizzes(); 
            }
            else
                Session["category_id"] = null;
        }



        private void showQuizzes()
        {
            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);

            webRef.Quiz[] quizCollection = Global.incuServ.GetAllQuizzes();
            quizs.Clear();
            if (category_id == 1)
            {
                foreach (webRef.Quiz q in quizCollection)
                {
                    if (q.category == c1)
                        quizs.Add(q);
                }
            }
            else if (category_id == 2)
            {
                foreach (webRef.Quiz q in quizCollection)
                {
                    if (q.category == c2)
                        quizs.Add(q);
                }
            }
            else if (category_id == 3)
            {
                foreach (webRef.Quiz q in quizCollection)
                {
                    if (q.category == c3)
                        quizs.Add(q);
                }
            }
            else
            {
                foreach (webRef.Quiz q in quizCollection)
                {
                    if (q.category == c0)
                        quizs.Add(q);
                }
            }
            tblQuizzes.Rows.Clear();
            //for each quiz
            foreach (webRef.Quiz q in quizs)
            {
                bool completed = (Global.incuServ.GetQuizStatus(c, q).answers.Length > 0);


                //build table entry for quiz
                LinkButton lnk = new LinkButton();
                lnk.Text = q.name;
                lnk.Attributes.Add("quiz_id", q.id.ToString());
                lnk.Click += new EventHandler(lnk_Click);
                lnk.ID = q.id.ToString();

                if (completed)
                {
                    lnk.Enabled = false;
                    lnk.Text += " (Completed)";
                }

                TableCell cell = new TableCell();
                cell.Controls.Add(lnk);
                TableRow tr = new TableRow();
                tr.Cells.Add(cell);
                this.tblQuizzes.Rows.Add(tr);
            }
           
}


        void lnk_Click(object sender, EventArgs e)
        {
            
            //TODO: WebServices call buy quiz
            Session["quizId"] = Int32.Parse(((WebControl)sender).Attributes["quiz_id"]);
            Session["questionNum"] = null;
            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);
            Global.incuServ.BuyQuiz(c,Global.incuServ.GetQuiz((int)Session["quizId"]));
            Response.Redirect("Quiz.aspx");
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            category_id = 0;
            Session["category_id"] = category_id;
            showQuizzes();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            category_id = 1;
            Session["category_id"] = category_id;
            showQuizzes();
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            category_id = 2;
            Session["category_id"] = category_id;
            showQuizzes();
        }

        protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {
            category_id = 3;
            Session["category_id"] = category_id;
            showQuizzes();
        }
    }
}